package cn.ibizlab.core.authentication.config;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
import static cn.ibizlab.core.authentication.constants.AuthenticationConstant.*;
import static com.google.code.kaptcha.Constants.*;

/**
 * 验证码配置
 */
@Configuration
@ConditionalOnExpression("'${ibiz.auth.imageCaptcha.enable:false}'.equals('true')")
public class SysAuthCaptchaConfig {
    @Value("${ibiz.auth.imageCaptcha.width:120}")
    private int imageWidth;
    @Value("${ibiz.auth.imageCaptcha.height:40}")
    private int imageHeight;
    @Value("${ibiz.auth.imageCaptcha.border:no}")
    private String border;
    @Value("${ibiz.auth.imageCaptcha.obscurificatorClass:com.google.code.kaptcha.impl.ShadowGimpy}")
    private String obscurificatorClass;
    @Value("${ibiz.auth.imageCaptcha.type:digits}")
    private String captchaType;
    @Value("${ibiz.auth.imageCaptcha.textProducerClass:com.google.code.kaptcha.text.impl.DefaultTextCreator}")
    private String defaultTextProducerClass;
    @Value("${ibiz.auth.imageCaptcha.digitsProducerClass:cn.ibizlab.core.authentication.config.DefaultDigitsCreator}")
    private String defaultDigitsProducerClass;
    @Bean
    public DefaultKaptcha captchaInit() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        // 是否有边框 默认为true 我们可以自己设置yes，no
        properties.setProperty(KAPTCHA_BORDER, border);
        // 边框颜色 默认为Color.BLACK
        properties.setProperty(KAPTCHA_BORDER_COLOR, "105,179,90");
        // 验证码文本字符颜色 默认为Color.BLACK
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
        // 验证码图片宽度 默认为200
        properties.put(KAPTCHA_IMAGE_WIDTH, String.valueOf(imageWidth));
        // 验证码图片高度 默认为50
        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, String.valueOf(imageHeight));
        // 验证码文本字符大小 默认为40
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "35");
        // 验证码文本生成器
        properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, LOGIN_CAPTCHA_TYPE_DIGITS.equalsIgnoreCase(captchaType)? defaultDigitsProducerClass : defaultTextProducerClass);
        // 验证码文本字符间距 默认为2
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");
        // 验证码文本字符长度 默认为5
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "6");
        // 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
        // 图片样式
        properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, obscurificatorClass);
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}
